<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?= $page_title ?></title>
  <?php include(APPLICATION_BASE_TPL_PATH . "/inc/include.phtml"); ?>
  <!--include-wechat-start-->
  <?php include(APPLICATION_BASE_TPL_PATH . "/inc/include_wechat.phtml"); ?>
  <!--include-wechat-end-->
</head>
<body>
<div id="wrapper">
  <?php echo $menuview; ?>
  <div id="page-wrapper" style="min-height: 0;">
    <div class="row">
      <div class="panel panel-primary">
        <div class="panel-heading">
          <h3 class="panel-title"><?= $page_header ?></h3>
        </div>
        <div class="panel-body">
          <form class="form-horizontal" name="filter" method="get">

            <div class="form-group" style="margin-bottom: 0">
              <label for="date-start" class="col-sm-2 control-label">当前微信号</label>
              <div class="col-sm-10">
                <?php foreach ($oa_list as $acc): ?>
                  <?php if ($acc['handling']): ?>
                    <button type="submit" name="oa_handling" value="<?= $acc['account'] ?>"
                            class="btn btn-success"><?= $acc['name'] ?>(<?= $acc['account'] ?>)
                      <span class="glyphicon glyphicon-ok"></span></button>
                  <?php else: ?>
                    <button type="submit" name="oa_handling" value="<?= $acc['account'] ?>"
                            class="btn btn-default"><?= $acc['name'] ?>(<?= $acc['account'] ?>)
                    </button>
                  <?php endif; ?>
                <?php endforeach; ?>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
    <div class="row">
      <div class="alert alert-wechat alert-info alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span
              class="sr-only">Close</span></button>
        <p><span class="glyphicon glyphicon-info-sign"></span>
          目前公众号在conf\security\ENV\wechat.php中配置,可以通过\Ext\Wechat\Util\AccountConfigurator中提供的方法获取.</p>
        <p><span class="glyphicon glyphicon-info-sign"></span>
          所谓"<strong>本地菜单数据</strong>"是指保存在本地数据库里的菜单,需要推送至微信服务器才能生效.</p>
      </div>

      <div class="panel panel-condensed panel-default panel-hover">
        <div class="panel-heading">
          <h5 class="panel-title">
            <span class="glyphicon glyphicon-list"></span>
            本地菜单数据

            <a role="button" data-oa="<?= $oa_handling ?>"
               class="btn btn-xxs btn-panel-title btn-danger pull-right menu-release"><span
                  class="glyphicon glyphicon-upload"></span>
              上传(发布)</a>

            <a role="button" data-action="create"
               class="btn btn-xxs btn-panel-title btn-success pull-right menu-edit"><span
                  class="glyphicon glyphicon-plus"></span>
              添加</a>
          </h5>
        </div>

        <?php if (empty($menu)): ?>
          <div class="panel-body">此账号目前没有设置菜单,点击右侧绿色"+添加"按钮添加菜单.</div>
        <?php else: ?>

          <table class="table table-bordered table-responsive table-condensed table-hover">
            <thead>
            <tr>
              <th>ID</th>
              <th>名称</th>
              <th>类型</th>
              <th>层级</th>
              <th>父菜单</th>
              <th>子菜单数量</th>
              <th>顺序</th>
              <th>状态</th>
              <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($menu as $item): ?>
              <tr class="bg-warning" style="color:black">
                <td data-id="<?= $item['id'] ?>"><?= $item['id'] ?></td>
                <td><?= $item['name'] ?></td>
                <td><?= $item['type_name'] ?></td>
                <td><?= $item['level_name'] ?></td>
                <td>--</td>
                <td><?= count($item['sub']) ?></td>
                <td><?= $item['sequence'] ?></td>

                <?php if ($item['is_deleted']): ?>
                  <td class="text-danger bg-danger"><?= $item['status_name'] ?></td>
                  <?php else: ?>
                  <td class="text-success bg-success"><?= $item['status_name'] ?></td>
                <?php endif; ?>

                <td style="text-align: left;">
                  <a role="button" data-action="update" data-menu='<?= json_encode($item) ?>'
                     class="btn btn-xxs btn-default menu-edit"><span class="glyphicon glyphicon-edit"></span>
                    修改</a>
                  <a role="button" data-action="create" data-menu='{"parent_id": "<?= $item['id'] ?>"}'
                     class="btn btn-xxs btn-default menu-edit"><span
                        class="glyphicon glyphicon-plus"></span>
                    添加子菜单</a>
                </td>
              </tr>

              <?php foreach ($item['sub'] as $sub): ?>
                <tr>
                  <td data-id="<?= $sub['id'] ?>"><?= $sub['id'] ?></td>
                  <td><?= $sub['name'] ?></td>
                  <td><?= $sub['type_name'] ?></td>
                  <td><?= $sub['level_name'] ?></td>
                  <td data-parent_id="<?= $sub['parent_id'] ?>">[<?= $sub['parent_id'] ?>][<?= $item['name'] ?>]</td>
                  <td><?= count($sub['sub']) ?></td>
                  <td><?= $sub['sequence'] ?></td>

                  <?php if ($sub['is_deleted']): ?>
                    <td class="text-danger bg-danger"><?= $sub['status_name'] ?></td>
                  <?php else: ?>
                    <td class="text-success bg-success"><?= $sub['status_name'] ?></td>
                  <?php endif; ?>

                  <td style="text-align: left;">
                    <a role="button" data-action="update" data-menu='<?= json_encode($sub) ?>'
                       class="btn btn-xxs btn-default menu-edit"><span class="glyphicon glyphicon-edit"></span>
                      修改</a>
                  </td>
                </tr>
              <?php endforeach; ?>
            <?php endforeach; ?>
            </tbody>
          </table>

        <?php endif; ?>
      </div>
    </div>


    <div class="row" style="display: none">
      <pre><?php #var_export($type_list); ?></pre>
      <pre><?php #var_export($menu); ?></pre>
      <pre><?php #var_export($oa_list); ?></pre>

    </div>
    <!-- /#page-wrapper -->

  </div>
  <!-- /#wrapper -->

  <!--添加或修改菜单的模态框-START-->
  <div class="modal fade" tabindex="-1" id="modal-edit-menu" role="dialog">
    <div class="modal-dialog modal-lg" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">根据data.action转换标题</h4>
        </div>
        <div class="modal-body">
          <div class="modal-loading clearfix" style="display: none;">
            <div class="spinner float-left"></div>
            <div class="modal-loading-message float-left">正在保存,请稍候……</div>
          </div>
          <div class="modal-message" style="display: none;"></div>
          <div class="modal-inner-content">
            <form class="form-horizontal">
              <input type="hidden" name="id" id="menu-id" value="">
              <input type="hidden" name="oa" id="menu-oa" value="<?= $oa_handling ?>">
              <div class="form-group">
                <label class="col-sm-2 control-label" for="menu-oa-show">所属微信号</label>
                <div class="col-sm-4">
                  <input disabled type="text" class="form-control" name="oa-show" id="menu-oa-show"
                         value="<?= $oa_handling ?>">
                </div>
                <div class="col-sm-5">
                  <p class="form-control-static text-warning">在修改或新建菜单之前确定</p>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label" for="menu-name">标题</label>
                <div class="col-sm-4">
                  <input type="text" class="form-control" name="name" id="menu-name" placeholder="菜单显示名">
                </div>
                <div class="col-sm-6">
                  <p class="form-control-static text-danger">必须:一级菜单的标题长度不超过16字节,二级菜单不超过40字节.</p>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label" for="menu-type">类型</label>
                <div class="col-sm-4">
                  <select class="form-control" id="menu-type" name="type">
                    <option value="0" selected>无(此菜单为一级菜单且有子菜单时选此项)</option>
                    <?php foreach ($type_list as $item): ?>
                      <option value="<?= $item['id'] ?>"><?= "{$item['id']}-{$item['val']}"; ?></option>
                    <?php endforeach; ?>
                  </select>
                </div>
                <div class="col-sm-6">
                  <p class="form-control-static text-danger">必须</p>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label" for="menu-key">Key</label>
                <div class="col-sm-4">
                  <input type="text" class="form-control" name="key" id="menu-key" placeholder="菜单KEY值">
                </div>
                <div class="col-sm-6">
                  <p class="form-control-static text-warning">click等点击类型必须:长度不超过128字节.</p>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label" for="menu-url">URL</label>
                <div class="col-sm-4">
                  <input type="text" class="form-control" name="url" id="menu-url" placeholder="网页链接">
                </div>
                <div class="col-sm-6">
                  <p class="form-control-static text-warning">view类型必须:长度不超过1024字节.</p>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label" for="menu-media_id">MediaId</label>
                <div class="col-sm-4">
                  <input type="text" class="form-control" name="media_id" id="menu-media_id" placeholder="永久素材ID">
                </div>
                <div class="col-sm-6">
                  <p class="form-control-static text-warning">media_id类型和view_limited类型必须</p>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label" for="menu-parent_id">父菜单</label>
                <div class="col-sm-4">
                  <select class="form-control" id="menu-parent_id" name="parent_id">
                    <option value="0" selected>此菜单为一级菜单</option>
                    <?php foreach ($top_list as $item): ?>
                      <option value="<?= $item['id'] ?>"><?= $item['name'] ?></option>
                    <?php endforeach; ?>
                  </select>
                </div>
                <div class="col-sm-6">
                  <p class="form-control-static text-warning"></p>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label" for="menu-sequence">顺序</label>
                <div class="col-sm-4">
                  <input type="text" class="form-control" name="sequence" id="menu-sequence" placeholder="永久素材ID">
                </div>
                <div class="col-sm-6">
                  <p class="form-control-static text-warning">数值越小越靠左\越靠上</p>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label" for="menu-status">状态</label>
                <div class="col-sm-4">
                  <select class="form-control" id="menu-status" name="status">
                    <?php foreach ($status_list as $item): ?>
                      <option value="<?= $item['id'] ?>"><?= $item['val'] ?></option>
                    <?php endforeach; ?>
                  </select>
                </div>
              </div>
            </form>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        </div>
      </div>
    </div>
  </div>
  <!--添加或修改菜单的模态框-END-->

</body>
<script type="text/javascript">

  $(function () {

    /*
     * 菜单的添加及修改
     */
    $('.menu-edit').modalExt({
      modal: 'modal-edit-menu',
      api: {
        create: ['POST', '<?php echo APP_ADMIN_PATH; ?>/wechat/menuCreate'],
        update: ['POST', '<?php echo APP_ADMIN_PATH; ?>/wechat/menuUpdate']
      },
      button: {
        save: {
          title: '保存', style: 'primary', callback: function (me, e) {
            var api = me.getData('action');
            if (!api) {
              console.error('undefined api name');
            }
            me.request(api, me.find$('form').serialize(), {
              success: function (me, res) {
                me.displayMessage(res.msg);
                if (2000000 == res.retcode) {
                  me.setButtons('ok');
                } else {
                  me.setButtons('cancel,retry');
                }
              }
            });
          }
        },
        cancel: {title: '取消', style: 'default', dismiss: true},
        ok: {
          title: '确定', style: 'primary', callback: function () {
            window.location.reload();
          }
        },
        retry: {
          title: '重试', style: 'primary', callback: function (me, e) {
            me.displayContent(true).setButtons('save,cancel');
          }
        }
      },
      callback: {
        init: function (me, e) {

        },
        show: function (me, e) {
          var action = me.getData('action');
          var menu = me.getData('menu') ? me.getData('menu') : {};
          var $form = me.find$('form').trigger('reset');
          $.each(menu, function (name, val) {
            $form.find('[name="' + name + '"]').val(val);
          });
          me.setTitle('create' == action ? (menu.parent_id ? '添加子菜单' : '添加菜单') : '修改菜单')
              .displayContent(true)
              .setButtons('save,cancel');
        },
        hidden: function (me) {
          me.setData('menu', null);
        }
      }
    });


    $('.menu-release').on('click', function () {

      var ajax = undefined;
      var $trigger = $(this);

      var req = function (notice) {


        console.log($trigger.data());

        ajax = $.ajax({
          type: 'POST',
          url: '<?php echo APP_ADMIN_PATH; ?>/wechat/menuRelease',
          data: $trigger.data(),
          beforeSend: function () {
            notice.content('正在发布,请稍候……').button(true, btn.abort);
          },
          success: function (res) {
            notice.content(res.msg);
            if (2000000 == res.retcode) {
              notice.button(true, btn.ok);
            } else {
              notice.button(true, btn.cancel, btn.retry);
            }
          },
          error: function () {
            notice.content('服务器异常,请稍后重试!').button(btn.ok);
          }
        });
        return ajax;
      };

      var btn = {
        confirm: {
          title: '确定', style: 'danger', callback: function (notice) {
            ajax = req(notice);
            return false;
          }
        },
        ok: {title: '确定', style: 'primary', dismiss: true},
        cancel: {title: '取消', style: 'default', dismiss: true},
        abort: {
          title: '中止', style: 'danger', callback: function (notice) {
            if (ajax) {
              ajax.abort();
            }
            notice.content('已尝试中止,是否已经将数据推送至微信服务器请以实际结果为准')
                .button(true, btn.ok);
          }
        },
        retry: {
          title: '重试', style: 'primary', callback: function (notice) {
            return req(notice);
          }
        }
      };

      var $notice = $('body').notice({
        style: 'info',
        show: true,
        content: '确定发布?',
        text_align: 'center',
        button: [btn.confirm, btn.cancel],
        interval: 0
      });


    });

  });
</script>
</html>
